package com.prj.ufdm.cloud.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 描述: 日期工具类
 * 
 * @author 胡义振
 * @date 2018-03-10
 */
public final class BaseDateUtil {

	/**
	 * 描述: 日期转为字符串
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		String result = "";
		if (date != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				result = sdf.format(date);
			} catch (Exception ex) {
				result = "";
			}
		}
		return result;
	}

	/**
	 * 获得当前日期的上个月最后一天
	 * 
	 * @return
	 */
	public static String getLastMonthOfLastDay() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		return BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 描述: 字符串转为日期类型
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param strDate
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String strDate, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(strDate);
		} catch (Exception er) {
			return null;
		}
	}

	/**
	 * 描述: 获取当前完整时间
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static String getCurrentTime() {
		return dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 描述: 获取当前年份
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static String getCurrentYear() {
		return dateToString(new Date(), "yyyy");
	}

	/**
	 * 描述: 获取当前月份
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static String getCurrentMonth() {
		return dateToString(new Date(), "MM");
	}

	/**
	 * 描述: 获取当前月份
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static int getCurrentIntMonth() {
		Calendar now = Calendar.getInstance();
		int currMonth = now.get(Calendar.MONTH) + 1;
		return currMonth;
	}

	/**
	 * 描述: 获取当前日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static String getCurrentDate() {
		return dateToString(new Date(), "yyyy-MM-dd");
	}

	/**
	 * 描述: 获取当前时间(时、分、秒)
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @return
	 */
	public static String getCurrentSimpleTime() {
		return dateToString(new Date(), "HH:mm:ss");
	}

	/**
	 * 描述: 把日期转成完整格式。如：2014-1-1 转化后为 2014-01-01
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param strDate
	 * @return
	 */
	public static String toComplexDate(String strDate) {
		try {
			String tmp_date[] = strDate.split("-");
			String tmp_year = tmp_date[0];
			String tmp_month = tmp_date[1];
			String tmp_day = tmp_date[2];
			if (Integer.parseInt(tmp_month) <= 9) {
				tmp_month = "0" + tmp_month;
			}
			if (Integer.parseInt(tmp_day) <= 9) {
				tmp_day = "0" + tmp_day;
			}
			return tmp_year + "-" + tmp_month + "-" + tmp_day;
		} catch (Exception er) {
			return strDate;
		}
	}

	/**
	 * 描述: 获取N年后日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param years
	 *            年数
	 * @return
	 */
	public static Date getAfterDateByYears(Date date, int years) {
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(date);
			now.add(Calendar.YEAR, years);
			return now.getTime();
		} catch (Exception er) {
			return date;
		}
	}

	/**
	 * 描述: 获取N月后日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param months
	 *            月数
	 * @return
	 */
	public static Date getAfterDateByMonths(Date date, int months) {
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(date);
			now.add(Calendar.MONTH, months);
			return now.getTime();
		} catch (Exception er) {
			return date;
		}
	}

	/**
	 * 描述: 获取N天后日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param months
	 *            月数
	 * @return
	 */
	public static Date getAfterDateByDays(Date date, int days) {
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(date);
			now.add(Calendar.DAY_OF_YEAR, days);
			return now.getTime();
		} catch (Exception er) {
			return date;
		}
	}

	/**
	 * 描述: 获取N小时后日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param hours
	 *            小时数
	 * @return
	 */
	public static Date getAfterDateByHours(Date date, int hours) {
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(date);
			now.add(Calendar.HOUR, hours);
			return now.getTime();
		} catch (Exception er) {
			return date;
		}
	}

	/**
	 * 描述: 获取N分钟后日期
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param date
	 * @param hours
	 *            小时数
	 * @return
	 */
	public static Date getAfterDateByMinutes(Date date, int minutes) {
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(date);
			now.add(Calendar.MINUTE, minutes);
			return now.getTime();
		} catch (Exception er) {
			return date;
		}
	}

	/**
	 * 描述: 计算两日期相差天数
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int getBetweenDays(Date startDate, Date endDate) {
		try {
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(startDate);
			long startTime = startCalendar.getTimeInMillis();
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(endDate);
			long endTime = endCalendar.getTimeInMillis();
			long between_days = (endTime - startTime) / (1000 * 60 * 60 * 24);
			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception er) {
			return 0;
		}
	}

	/**
	 * 描述: 计算两日期相差分钟
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int getBetweenMinutes(Date startDate, Date endDate) {
		try {
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(startDate);
			long startTime = startCalendar.getTimeInMillis();
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(endDate);
			long endTime = endCalendar.getTimeInMillis();
			long between_days = (endTime - startTime) / (1000 * 60);
			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception er) {
			return 0;
		}
	}

	/**
	 * 描述: 计算两日期相差秒钟
	 * 
	 * @auther 胡义振
	 * @date 2018-03-10
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int getBetweenSeconds(Date startDate, Date endDate) {
		try {
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(startDate);
			long startTime = startCalendar.getTimeInMillis();
			Calendar endCalendar = Calendar.getInstance();
			endCalendar.setTime(endDate);
			long endTime = endCalendar.getTimeInMillis();
			long between_seconds = (endTime - startTime) / 1000;
			return Integer.parseInt(String.valueOf(between_seconds));
		} catch (Exception er) {
			return 0;
		}
	}

	/**
	 * 获取两个时间段之间取每个年月
	 * 
	 * @param minDate
	 *            最小时间 2018-01
	 * @param maxDate
	 *            最大时间 2018-10
	 * @return
	 */
	public static List<String> getMonthBetween(String minDate, String maxDate) {
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");// 格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		try {
			min.setTime(sdf.parse(minDate));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

			max.setTime(sdf.parse(maxDate));
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
		} catch (ParseException e) {
			return result;
		}

		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf1.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}

		return result;
	}

	/**
	 * 获取去年开始日期
	 * 
	 * @return
	 */
	public static String getQnksrq() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, -1);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String qnksrq = BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
		return qnksrq;
	}

	/**
	 * 获取三年日期
	 * 
	 * @return
	 */
	public static String getKsrq() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, -3);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String qnksrq = BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
		return qnksrq;
	}

	/**
	 * 获取三月前年月，比如现在2019-03，则获取的为2019-01
	 * 
	 * @return
	 */
	public static String getSyqNy(String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -2);
		String syqNy = BaseDateUtil.dateToString(cal.getTime(), format);
		return syqNy;
	}

	/**
	 * 获取三月前年月，比如现在2019-02，则获取的为2019-01
	 * 
	 * @return
	 */
	public static String getEyqNy(String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		String eyqNy = BaseDateUtil.dateToString(cal.getTime(), format);
		return eyqNy;
	}

	/**
	 * 获取开始日期，也就是去年开始日期
	 * 
	 * @return
	 */
	public static String getBnksrq() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, 0);
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String qnksrq = BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
		return qnksrq;
	}

	/**
	 * 获取三个月前日期，按照100天算
	 * 
	 * @return
	 */
	public static String getSyqrq() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -100);// 设置为1号,当前日期既为本月第一天
		String syqrq = BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
		return syqrq;
	}

	/**
	 * 获取5个月前日期，按照一个月30天算
	 * 
	 * @return
	 */
	public static String getWyqrq() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -150);// 设置为1号,当前日期既为本月第一天
		String syqrq = BaseDateUtil.dateToString(cal.getTime(), "yyyy-MM-dd");
		return syqrq;
	}
	
	/**
	 * 获取某个日期月的天数
	 * @param date
	 * @return
	 */
	public static int getDayBymonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获取当前日期往前推一年的所有年月
	 * 
	 * @Title: getDateByYear
	 * @Description: TODO
	 * @return     @return List<String> 
	 */
	public static List<String> getDateByYear() {
		List<String> list = new ArrayList<>();
		for (int i = 0; i < 12; i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("YYYYMM");
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());
			c.add(Calendar.MONTH, -i);
			Date m = c.getTime();
			list.add(sdf.format(m));
		}
		return list;
	}

	/**
	 * 获取N月前的日期
	* @Title: getYearMonths
	* @Description: TODO
	* @param i
	* @return    
	* @return String 
	 */
	public static String getYearMonths(int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -i);
		Date m = c.getTime();
		return sdf.format(m);
	}

	public static void main(String str[]) {

		// Date year16 = getAfterDateByYears(new Date(), -65);
		//
		// System.out.println(dateToString(year16, "yyyy-MM-dd"));
		//
		// System.out.println(Long.valueOf("-"+"16"));
		//
		// List<String> list = getMonthBetween("2018-09","2018-09");
		// System.out.println(list.size());

		// SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// Calendar c = Calendar.getInstance();
		// c.setTime(new Date());
		// c.add(Calendar.MONTH, -24);
		// Date m = c.getTime();

		// System.out.println("---->"+getKsrq());


		SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); 
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
        c.add(Calendar.MONTH, -6);
        Date m = c.getTime();
		System.out.println(format.format(m));

	}

}
